package com.hxrfid.sms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxrfid.common.core.domain.BaseEntity;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.utils.StringUtils;
import com.hxrfid.common.utils.TenantUtils;
import com.hxrfid.sms.domain.SysSmsRecord;
import com.hxrfid.sms.domain.bo.SysSmsRecordBo;
import com.hxrfid.sms.domain.vo.SysSmsRecordVo;
import com.hxrfid.sms.mapper.SysSmsRecordMapper;
import com.hxrfid.sms.service.ISysSmsRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 短信发送记录Service业务层处理
 *
 * @author hxrfid
 * @date 2022-12-26
 */
@RequiredArgsConstructor
@Service
public class SysSmsRecordServiceImpl extends ServiceImpl<SysSmsRecordMapper, SysSmsRecord> implements ISysSmsRecordService {

    private final SysSmsRecordMapper baseMapper;

    /**
     * 查询短信发送记录
     * @param id 主键ID
     * @return 短信发送记录
     */
    @Override
    public SysSmsRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询短信发送记录列表
     * @param bo 短信发送记录对象
     * @param pageQuery 主键ID
     * @return 短信发送记录列表
     */
    @Override
    public TableDataInfo<SysSmsRecordVo> queryPageList(SysSmsRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSmsRecord> lqw = buildQueryWrapper(bo);
        Page<SysSmsRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询短信发送记录列表
     * @param bo 短信发送记录对象
     * @return 短信发送记录列表
     */
    @Override
    public List<SysSmsRecordVo> queryList(SysSmsRecordBo bo) {
        LambdaQueryWrapper<SysSmsRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
    * 组装查询条件
    * @param bo 短信发送记录对象
    * @return 查询条件
    */
    private LambdaQueryWrapper<SysSmsRecord> buildQueryWrapper(SysSmsRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSmsRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getConfigId() != null, SysSmsRecord::getConfigId, bo.getConfigId());
        lqw.like(StringUtils.isNotBlank(bo.getPhoneNumber()), SysSmsRecord::getPhoneNumber, bo.getPhoneNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSuccessStatus()), SysSmsRecord::getSuccessStatus, bo.getSuccessStatus());
        lqw.like(StringUtils.isNotBlank(bo.getContent()), SysSmsRecord::getContent, bo.getContent());
        lqw.like(StringUtils.isNotBlank(bo.getFailReason()), SysSmsRecord::getFailReason, bo.getFailReason());
        lqw.eq(ObjectUtil.isNotNull(bo.getTenantId()) && !TenantUtils.EMPTY_TENANT_ID.equals(bo.getTenantId()), SysSmsRecord::getTenantId, bo.getTenantId());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            SysSmsRecord::getSendTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.orderByDesc(BaseEntity::getCreateTime);
        return lqw;
    }

    /**
     * 新增短信发送记录
     * @param bo 短信发送记录对象
     * @return id
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Long insertByBo(SysSmsRecordBo bo) {
        SysSmsRecord add = BeanUtil.toBean(bo, SysSmsRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return bo.getId();
    }

    /**
     * 修改短信发送记录
     * @param bo 短信发送记录对象
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateByBo(SysSmsRecordBo bo) {
        SysSmsRecord update = BeanUtil.toBean(bo, SysSmsRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     * @param entity 短信发送记录对象
     */
    private void validEntityBeforeSave(SysSmsRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除短信发送记录
     * @param ids 主键ID的list
     * @param isValid 是否业务校验
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
